package priv.pront.code.national.acwing.dp.tree;

import java.util.*;
import java.io.*;

/**
 * @Description:
 * @Author: pront
 * @Time:2023-05-23 19:48
 */
public class Acwing1073_树的中心 {

    static int INF = Integer.MAX_VALUE;
    static int n, m;
    static int[] h;
    static int[] e;
    static int[] ne;
    static int[] w;
    static int idx = 0;
    static int[] d1;
    static int[] d2;
    static int[] up;
    static int[] p1; //p1[i] d1 node
    static int[] p2;// p2[i] d2 node
    static BufferedReader read = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException {
        String[] s = read.readLine().split("\\s+");
        n = Integer.parseInt(s[0]);
        m = 2 * n;
        h = new int[n + 1];
        e = new int[m + 1];
        ne = new int[m + 1];
        w = new int[m + 1];
        d1 = new int[n + 1];
        d2 = new int[n + 1];
        up = new int[n + 1];
        p1 = new int[n + 1];
        p2 = new int[n + 1];
        Arrays.fill(h, -1);
        for (int i = 0; i < n - 1; i++) {
            s = read.readLine().split("\\s+");
            int a = Integer.parseInt(s[0]);
            int b = Integer.parseInt(s[1]);
            int c = Integer.parseInt(s[2]);
            add(a, b, c);
            add(b, a, c);

        }
        dfs_d(1, -1);
        dfs_up(1, -1);

        int ans = INF;
        for (int i = 1; i <= n; i++) {
            ans = Math.min(ans, Math.max(d1[i], up[i]));
        }
        System.out.println(ans);

    }

    private static int dfs_d(int u, int father) {
        for (int j = h[u]; j != -1; j = ne[j]) {
            int i = e[j];
            if (i == father) continue;
            int d = dfs_d(i, u) + w[j];
            if (d > d1[u]) {
                d2[u] = d1[u];
                d1[u] = d;
                p2[u] = p1[u];
                p1[u] = i;
            } else if (d > d2[u]) {
                d2[u] = d;
                p2[u] = i;
            }
        }
        return d1[u];
    }

    private static void dfs_up(int u, int father) {
        for (int i = h[u]; i != -1; i = ne[i]) {
            int j = e[i];
            if (j == father) continue;
            if (p1[u] == j) up[j] = Math.max(up[u], d2[u]) + w[i];
            else up[j] = Math.max(up[u], d1[u]) + w[i];
            dfs_up(j, u);
        }
    }

    static void add(int a, int b, int c) {
        e[idx] = b;  // add node
        w[idx] = c;  // add weight
        ne[idx] = h[a];
        h[a] = idx++; // move pointer
    }

}
